using System.Data;
using System.Data.SqlClient;

namespace NetMigrationTests
{
    public class DB
    {
        public DB(string connectionString)
        {
            this.connectionString = connectionString;
        }

        private readonly string connectionString;

        public DataTable ExecuteDataTable(string commandText)
        {
            var con = GetConnection();
            var adapter =
                new SqlDataAdapter(commandText,
                                   con);
            var dt = new DataTable();

            adapter.Fill(dt);

            con.Close();
            return dt;
        }

        public void ExecuteCommand(string commandText)
        {
            var con = GetConnection();
            var cmd = new SqlCommand(commandText, con);

            cmd.ExecuteNonQuery();

            con.Close();
        }

        private SqlConnection GetConnection()
        {
            var con = new SqlConnection(connectionString);
            if (con.State != ConnectionState.Open)
            {
                con.Open();
            }
            return con;
        }
    }
}